package com.mall.order.biz.handler;

import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dto.CartProductDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *  ciggar
 * create-date: 2019/8/1-下午5:01
 * 初始化订单 生成订单
 */

@Slf4j
@Component
public class InitOrderHandler extends AbstractTransHandler {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        CreateOrderContext orderContext = (CreateOrderContext) context;
        String orderId = UUID.randomUUID().toString();
        //插入一条订单，如果插入失败返回false
        if (!insetOrder(orderContext,orderId)) return false;
        orderContext.setOrderId(orderId);
        ArrayList<Long> buyProductIds = new ArrayList<>();

        //插入N条订单商品，并设置contest，如果插入失败返回false
        if (!insertOrderItemList(orderContext, orderId,buyProductIds)) return false;

        return true;
    }

    /**
     * 插入N条订单商品，如果插入失败返回false
     * @param orderContext
     * @param orderId
     * @return
     */
    private boolean insertOrderItemList(CreateOrderContext orderContext, String orderId,List<Long> buyProductIds) {
        List<OrderItem> orderItemList = new ArrayList<>();
        List<CartProductDto> cartProductDtoList = orderContext.getCartProductDtoList();
        for (CartProductDto productDto : cartProductDtoList) {
            OrderItem orderItem = new OrderItem();
            orderItem.setId(String.valueOf(System.currentTimeMillis()));
            orderItem.setItemId(productDto.getProductId());
            orderItem.setOrderId(orderId);
            orderItem.setNum(productDto.getProductNum().intValue());
            orderItem.setTitle(productDto.getProductName());
            orderItem.setPrice(productDto.getSalePrice().doubleValue());
            orderItem.setTotalFee(orderContext.getOrderTotal().doubleValue());
            orderItem.setPicPath(productDto.getProductImg());
            orderItem.setStatus(1);
            orderItemMapper.insert(orderItem);

            orderItemList.add(orderItem);
            buyProductIds.add(productDto.getProductId());
        }

        //int i = orderItemMapper.insertList(orderItemList);
//        if (i == 0) return false;
        orderContext.setBuyProductIds(buyProductIds);
        orderContext.setOrderId(orderId);
        return true;
    }

    /**
     * 插入一条订单，如果插入失败返回false
     * @param orderContext
     * @return
     */
    private boolean insetOrder(CreateOrderContext orderContext,String orderId) {

        Order order = new Order();
        Date date = new Date();
        order.setOrderId(orderId);
        order.setPayment(orderContext.getOrderTotal());
        order.setPaymentType(1);
        order.setStatus(OrderConstants.ORDER_STATUS_INIT);
        order.setCreateTime(date);
        order.setUpdateTime(date);
        order.setUserId(orderContext.getUserId());
        order.setBuyerNick(orderContext.getBuyerNickName());
        int i = orderMapper.insertSelective(order);
        if (i == 0) return false;
        return true;
    }
}
